package service;

import java.util.GregorianCalendar;
import java.util.List;

import dao.MachineTypeDao;
import dao.SparePartDao;
import model.*;

public class Service {
	
	// Singleton Pattern...
	
	// The static Object.
	private static Service instance = new Service();

	// The method for getting the instance.
	public static Service getInstance() {
		return instance;
	}

	// The private constructor.
	private Service() {
		startUp();
	}
	
	// ...Singleton Pattern

	public void startUp() {
		MachineType girdmester = new MachineType(1111111, "Girdmester",
				"power machine");
		MachineType borsch = new MachineType(1111112, "Borsch", "power machine");
		MachineType vetax = new MachineType(1111113, "Vetax", "power machine");

		Machine m1 = new Machine(1000000, "GS1", "Bending machine", girdmester);
		@SuppressWarnings("unused")
		Machine m2 = new Machine(1000001, "GS2", "Straight machine", girdmester);
		@SuppressWarnings("unused")
		Machine m3 = new Machine(1000002, "GS3", "Triangle machine", girdmester);
		Machine m4 = new Machine(1000011, "BO1", "Circle machine", borsch);
		@SuppressWarnings("unused")
		Machine m5 = new Machine(1000012, "BO3", "Zigzag machine", borsch);
		@SuppressWarnings("unused")
		Machine m6 = new Machine(2000011, "VE1", "Breaking machine", vetax);
		@SuppressWarnings("unused")
		Machine m7 = new Machine(2000012, "VE2", "Twisting machine", vetax);

		// --------------------------------------------------------
		SparePart sp1 = new SparePart(1111111, 111, 111, 000, "sp1-ring26",
				123, 120, 25, "for GS20");
		SparePart sp2 = new SparePart(2202334, 331, 311, 321, "sp2-", 30, 32,
				7, "for GS32");
		SparePart sp3 = new SparePart(1111113, 111, 113, 000, "sp3-ring28", 41,
				32, 180, "for GS20");
		SparePart sp4 = new SparePart(1111114, 111, 114, 000, "sp4-ring29", 63,
				50, 5, "for GS20");
		SparePart sp5 = new SparePart(1111115, 111, 115, 000, "sp5-transistor",
				20, 16, 90, "for GS20");
		SparePart sp6 = new SparePart(2203459, 331, 311, 321, "sp6-Sp", 30, 32,
				7, "for GS20");

		girdmester.addSparePart(sp1);
		girdmester.addSparePart(sp2);
		borsch.addSparePart(sp4);
		vetax.addSparePart(sp3);
		vetax.addSparePart(sp5);
		vetax.addSparePart(sp6);
		// --------------------------------------------------------
		RepairType rt1 = new RepairType(1010101, "AAA", "Power upgrade");
		girdmester.addRepairType(rt1);
		rt1.addSparePartUse(new SparePartUse(10, sp1));
		rt1.addSparePartUse(new SparePartUse(2, sp2));

		RepairType rt2 = new RepairType(1010102, "AAB", "Pressure turbulence");
		borsch.addRepairType(rt2);
		rt2.addSparePartUse(new SparePartUse(1, sp4));
		rt2.addSparePartUse(new SparePartUse(2, sp5));

		RepairType rt3 = new RepairType(1010102, "AAC", "Pressure turbulence2");
		borsch.addRepairType(rt3);
		rt3.addSparePartUse(new SparePartUse(1, sp3));
		rt3.addSparePartUse(new SparePartUse(2, sp6));

		rt2.addRepairTypes(rt3);
		// --------------------------------------------------------
		GregorianCalendar d1 = new GregorianCalendar(2008,
				GregorianCalendar.MAY, 12, 16, 20, 20);
		Reparation r1 = new Reparation(3132123, d1, "description", m1);

		r1.addRepairType(rt1);
		r1.addTempSparePartUse(new SparePartUse(12, sp1));
		r1.addTempSparePartUse(new SparePartUse(2, sp1));
		r1.removeTempSparePartUse(new SparePartUse(13, sp1));
		r1.addTempSparePartUse(new SparePartUse(4, sp2));
		r1.addTempSparePartUse(new SparePartUse(2, sp3));
		r1.update();

		r1.endReparation(new GregorianCalendar(2008, GregorianCalendar.MAY, 21,
				18, 24, 53));
		// --------------------------------------------------------
		GregorianCalendar d2 = new GregorianCalendar(2008,
				GregorianCalendar.JANUARY, 1, 13, 30, 20);
		Reparation r2 = new Reparation(1234567, d2, "mullfunction", m4);

		r2.addRepairType(rt2);
		r2.addTempSparePartUse(new SparePartUse(12, sp4));
		r2.addTempSparePartUse(new SparePartUse(2, sp3));
		r2.removeTempSparePartUse(new SparePartUse(11, sp4));
		r2.addTempSparePartUse(new SparePartUse(4, sp5));
		r2.addTempSparePartUse(new SparePartUse(2, sp6));
		r2.update();

		// r2.endReparation(new GregorianCalendar(2008, GregorianCalendar.MAY,
		// 24,
		// 8, 43, 43));
		// --------------------------------------------------------
		addSparePart(sp1);
		addSparePart(sp2);
		addSparePart(sp3);
		addSparePart(sp4);
		addSparePart(sp5);
		addSparePart(sp6);
		addMachineType(girdmester);
		addMachineType(borsch);
		addMachineType(vetax);
	}

	/**
	 * Link to MachineTypeDao class
	 * 
	 * @author Osahon Idehen
	 */

	private MachineTypeDao machineTypeDao = MachineTypeDao.getInstance();

	/**
	 * Link to SparePartDao
	 * 
	 * @author Osahon Idehen
	 */

	private SparePartDao sparePartDao = SparePartDao.getInstance();

	/**
	 * This creates MachineType and adds to the machineTypeDao
	 * 
	 * @author Osahon Idehen
	 */

	public void addMachineType(MachineType machineType) {
		machineTypeDao.addMachineType(machineType);
	}

	/**
	 * This removes MachineType from the machineTypeDao
	 * 
	 * @author Osahon Idehen
	 */

	public void removeMachineType(MachineType machineType) {
		machineTypeDao.removeMachineType(machineType);
	}

	/**
	 * This returns the MachineTypes from machineTypeDao
	 * 
	 * @author Osahon Idehen
	 */

	public List<MachineType> getMachineTypes() {
		return machineTypeDao.getMachineTypes();
	}

	/**
	 * This creates the SpareParts and adds it to the sparePartDao
	 * 
	 * @author Osahon Idehen
	 */

	public void addSparePart(SparePart sparePart) {
		sparePartDao.addSparePart(sparePart);
	}

	/**
	 * This removes the spareParts from the SparePartDao
	 * 
	 * @author Osahon Idehen
	 */

	public void removeSparePart(SparePart sparePart) {
		sparePartDao.removeSparePart(sparePart);
	}

	/**
	 * This returns the SpareParts from the sparePartDao
	 * 
	 * @author Osahon Idehen
	 */
	public List<SparePart> getSpareParts() {
		return sparePartDao.getSpareParts();
	}

	// TODO extra methods
	/**
	 * Gets all the Machines from all the MachineTypes.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public List<Machine> getMachines() {
		return machineTypeDao.getMachines();
	}

	/**
	 * Gets all the Reparations from all the Machines.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public List<Reparation> getReparations() {
		return machineTypeDao.getReparations();
	}

	/**
	 * Gets all the RepairTypes from all the Machines.
	 * 
	 * @author Csaba Tarkanyi
	 */
	public List<RepairType> getRepairTypes() {
		return machineTypeDao.getRepairTypes();
	}

	// TODO extra methods
}